package com.sen.basecode.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sen.basecode.mapper.BaseCodeMapper;
import com.sen.basecode.model.BaseCode;
import com.sen.basecode.model.BaseCodeAttribute;
import com.sen.basecode.service.BaseCodeAttributeService;
import com.sen.basecode.service.BaseCodeService;
import com.sen.basecode.vo.BaseCodeAttributeVO;
import com.sen.basecode.vo.BaseCodeVO;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：linsir
 * @date ：Created in 2021/6/21 17:02
 * @description：接口实现
 * @modified By：
 * @version: 1.0.0
 */

@Service
public class BaseCodeServiceImpl extends ServiceImpl<BaseCodeMapper, BaseCode> implements IService<BaseCode>, BaseCodeService {

    @Autowired
    private  BaseCodeAttributeService baseCodeAttributeService;

    @Resource
    private ModelMapper modelMapper;

    @Override
    public BaseCodeVO getBaseCodeAttribute(String fieldCode) {
        BaseCodeVO baseCodeVO = new BaseCodeVO();
        List<BaseCodeAttribute> baseCodeAttributeList = baseCodeAttributeService.getListByFieldCode(fieldCode);
        if (CollectionUtil.isNotEmpty(baseCodeAttributeList))
        { baseCodeVO.setBaseCodeAttributeVOList(baseCodeAttributeList.stream()
                .map(attribute->modelMapper
                        .map(attribute, BaseCodeAttributeVO.class))
                .collect(Collectors.toList())); }
        return baseCodeVO;
    }
}
